package com.caojing.syby.ui.main;

import android.content.DialogInterface;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.caojing.syby.R;
import com.caojing.syby.base.BaseActivity;
import com.caojing.syby.ui.account.AccountFragment;
import com.caojing.syby.ui.brandsale.BrandsaleFragment;
import com.caojing.syby.ui.cart.CartFragment;
import com.caojing.syby.ui.home.HomeFragment;
import com.caojing.syby.ui.tenbuy.TenbuyFragment;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements MainContract.View {

    @Bind(R.id.main_activity_container)
    FrameLayout mainActivityContainer;
    @Bind(R.id.main_activity_rg)
    RadioGroup mainActivityRg;
    private FragmentManager manager;
    private HomeFragment homeFragment;
    private BrandsaleFragment brandsaleFragment;
    private TenbuyFragment tenbuyFragment;
    private CartFragment cartFragment;
    private AccountFragment accountFragment;
    private final int HOME=0,BRAND=1,TEN=2,CART=3,ACCOUNT=4;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        homeFragment = new HomeFragment();
        brandsaleFragment = new BrandsaleFragment();
        tenbuyFragment = new TenbuyFragment();
        cartFragment = new CartFragment();
        accountFragment = new AccountFragment();
        ft.add(R.id.main_activity_container,homeFragment)
                .add(R.id.main_activity_container,brandsaleFragment)
                .add(R.id.main_activity_container,tenbuyFragment)
                .add(R.id.main_activity_container,cartFragment)
                .add(R.id.main_activity_container,accountFragment)
                .commit();

        selectFragment(HOME);

        mainActivityRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId){
                    case R.id.main_activity_rg_0:
                        selectFragment(HOME);
                        break;
                    case R.id.main_activity_rg_1:
                        selectFragment(BRAND);
                        break;
                    case R.id.main_activity_rg_2:
                        selectFragment(TEN);
                        break;
                    case R.id.main_activity_rg_3:
                        selectFragment(CART);
                        break;
                    case R.id.main_activity_rg_4:
                        selectFragment(ACCOUNT);
                        break;
                }
            }
        });

    }

    private void selectFragment(int position) {
        FragmentTransaction ft1 = manager.beginTransaction();
        ft1.hide(homeFragment).hide(brandsaleFragment).hide(tenbuyFragment)
                .hide(cartFragment).hide(accountFragment);
        mainActivityRg.getChildAt(HOME).setSelected(false);
        mainActivityRg.getChildAt(BRAND).setSelected(false);
        mainActivityRg.getChildAt(TEN).setSelected(false);
        mainActivityRg.getChildAt(CART).setSelected(false);
        mainActivityRg.getChildAt(ACCOUNT).setSelected(false);
        switch (position){
            case HOME:
                ft1.show(homeFragment);
                mainActivityRg.getChildAt(HOME).setSelected(true);
                break;
            case BRAND:
                ft1.show(brandsaleFragment);
                mainActivityRg.getChildAt(BRAND).setSelected(true);
                break;
            case TEN:
                ft1.show(tenbuyFragment);
                mainActivityRg.getChildAt(TEN).setSelected(true);
                break;
            case CART:
                ft1.show(cartFragment);
                mainActivityRg.getChildAt(CART).setSelected(true);
                break;
            case ACCOUNT:
                ft1.show(accountFragment);
                mainActivityRg.getChildAt(ACCOUNT).setSelected(true);
                break;
        }
        ft1.commit();
    }

    @Override
    protected void initData() {

    }
    //退出程序提示框
    public void onBackPressed() {
        new AlertDialog.Builder(this).setTitle("确认退出吗？")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击“确认”后的操作
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("返回", null).show();
    }


}
